package com.alinesno.cloud.common.web.basic.auth.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.common.core.monitor.Server;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.facade.response.ResponseGenerator;
import com.alinesno.cloud.common.web.basic.auth.entity.BasicResourceEntity;
import com.alinesno.cloud.common.web.basic.auth.repository.BasicResourceRepository;


/**
 * 控制层
 * @author WeiXiaoJin
 * @since 2018年11月27日 上午6:41:40
 */
@Controller
public class HttpBasicDashboardController {

	private static final Logger log = LoggerFactory.getLogger(HttpBasicDashboardController.class) ; 

	@Value("${alinesno.basic.title:应用管理平台}")
	private String basicTitle ; 
	
	@Value("${alinesno.page.home:dashboard/home}")
	private String pageHome ; 

	@Autowired
	private BasicResourceRepository resourceRepository ; 
	
	@ResponseBody
	@GetMapping("/dashboard/side")
    public ResponseBean side(String resourceParent , String applicationId){
		log.debug("resourceParant:{} , applicationId:{}" , resourceParent , applicationId);
		return ResponseGenerator.ok(null); 
    }
	
	@RequestMapping("/dashboard")
    public String dashboard(Model model , ModelMap mmap) throws Exception{
		log.debug("dashboard:{}" , model);
		
		List<BasicResourceEntity> list = resourceRepository.findAllByHasStatusOrderByResourceOrderAsc(0) ; 
		
		model.addAttribute("menus" , list) ;
		model.addAttribute("applicationTitle", basicTitle) ;  
		return "dashboard/dashboard" ; 
    }
	
	@RequestMapping("/dashboard/home")
    public String home(ModelMap mmap) throws Exception{
		log.debug("home:{}" , pageHome);
	
		Server server = new Server();
		server.copyTo();
		
		mmap.put("server", server);
		
		return pageHome ;
    }
	
	@RequestMapping("/")
    public String index(){
		log.debug("index");
		return "redirect:/dashboard" ;
    }
	
	@RequestMapping("/login")
    public String login(){
		log.debug("basic login");
		return "basic/login" ; 
    }

	/**
	 * 公共下载
	 * @param request
	 * @param fileName
	 * @param delete
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/dashboard/common/download")
	public ResponseEntity<byte[]> download(HttpServletRequest request, String fileName , String delete) throws IOException {

		File file = new File(fileName) ; 
		byte[] body = FileUtils.readFileToByteArray(file) ; 
		
		if(Boolean.parseBoolean(delete)) {
			FileUtils.forceDelete(file);
		}
		
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Disposition", "attchement;filename=" + fileName) ;
		HttpStatus statusCode = HttpStatus.OK;
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
		return entity;
	}
}
